//
//  ST_AICenterMemberCell.swift
//  btvideo
//
//  Created by mac on 2020/5/7.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwifterSwift
import SnapKit




class ST_AICenterMemberCell: UICollectionViewCell {

    static let height = (632+194-80).cgFloat
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.addSubview(member_background_view)
        member_background_view.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.top.equalToSuperview()
            make.height.equalTo(94)
        }
        
        
        member_background_view.addSubview(avatar_image_view)
        avatar_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(44)
            make.left.equalToSuperview().offset(20)
            make.centerY.equalToSuperview()
        }
        
        
        member_background_view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(avatar_image_view)
            make.left.equalTo(avatar_image_view.snp.right).offset(10)
            make.height.equalTo(25)
        }
        
        member_background_view.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(2)
            make.left.equalTo(titleLabel)
            make.height.equalTo(17)
        }
        
        member_background_view.addSubview(click_label)
        click_label.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-35)
            make.height.equalTo(32)
            make.width.equalTo(86)
        }
        
        contentView.addSubview(member_feature_view)
        member_feature_view.snp.makeConstraints { (make) in
            make.top.equalTo(member_background_view.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(ST_MemberFeatureView.height)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
//    MARK: - Properties
    fileprivate lazy var member_background_view:UIButton = {
        let control = UIButton.init(type: UIButton.ButtonType.custom)
        control.set_back_image_for_all_states(ST_ImageUtils.imageName("个人中心_img_会员bg")!)
        control.setBackgroundImage(ST_ImageUtils.imageName("个人中心_img_非会员bg"), for: UIControl.State.normal)
        control.add_touch_up(insideTarget: self, action: #selector(pay_member_handle))
        return control
    }()
    

    fileprivate lazy var avatar_image_view:UIImageView = {
         let control = UIImageView()
          control.contentMode = .scaleAspectFill
          control.cornerRadius = 22
        control.borderWidth = 1
        control.borderColor = .white
          return control
      }()

    
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        return control
    }()
    
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        return control
    }()
    
    
    fileprivate lazy var click_label:UILabel = {
        let control = UILabel.init()
        control.cornerRadius = 13
        control.textAlignment = .center
        return control
    }()
    
    fileprivate lazy var member_feature_view = ST_MemberFeatureView()
    
}

extension ST_AICenterMemberCell{
    
    @objc fileprivate func pay_member_handle(){
        
        
        if FETCH_MEMBER_STATUS() == .subscribe{
            return
        }
        
        ST_MaidWayManager.shared.source = .我的
        
        let vc  = ST_MemberSubscriberController.init(subscribeType: ST_BitianSubscribeType.无线喜欢次数)

        vc.overCurrentContext = true

        GlobalPresent(toController: vc)
        
    }
    
}

extension ST_AICenterMemberCell{
    
    
    func refresh_member_status(){
        
        guard let userInfo = ST_BTUserInfoManager.shared.current_account_info?.userinfo else { return }
                
        avatar_image_view.pin_setImage(from: userInfo.avatar.compress_small_image(width: 120).url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        
        let nickName = NSMutableAttributedString(string: userInfo.nickname)

        nickName.addAttributes([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 18, weight: UIFont.Weight.medium),NSAttributedString.Key.foregroundColor: UIColor.init(hex: 0x4B3800)!], range: NSRange(location: 0, length: nickName.length))
        
        let content_attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 12),.foregroundColor: UIColor.init(hex: 0x4B3800)!]
        
        
        let status = FETCH_MEMBER_STATUS()
        
        switch status {
        case .subscribe:
            
            member_background_view.isSelected = true
            avatar_image_view.borderColor = UIColor.init(hex: 0xFFD654)
         
            let dayStr = Date.init(unixTimestamp: userInfo.vip_expire.double).string(withFormat: "yyyy.MM.dd")
            
            let attrString = NSMutableAttributedString(string: "有效期至\(dayStr)")
            
            
            attrString.addAttributes(content_attr, range: NSRange(location: 0, length: attrString.length))
            
            content_label.attributedText = attrString
       
            click_label.isHidden = true
            break
        case .expired:
            
            member_background_view.isSelected = false
            avatar_image_view.borderColor = UIColor.white
            
            nickName.addAttributes([NSAttributedString.Key.foregroundColor: UIColor.init(hex: 0x333333)!], range: NSRange(location: 0, length: nickName.length))
            
            
            let attrString = NSMutableAttributedString(string: "会员服务已到期")
            
            attrString.addAttributes(content_attr, range: NSRange(location: 0, length: attrString.length))
            content_label.attributedText = attrString
            
            
            
            let btnAttrString = NSMutableAttributedString(string: "续费会员")
            let attr1: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium),.foregroundColor: UIColor.init(hex: 0xFFD654)!]
            btnAttrString.addAttributes(attr1, range: NSRange(location: 0, length: btnAttrString.length))
            click_label.attributedText = btnAttrString
            click_label.backgroundColor = UIColor.init(hex: 0x2C211D)
            click_label.isHidden = false
            
            break
        default:
            
            member_background_view.isSelected = false
            avatar_image_view.borderColor = UIColor.white
            
            nickName.addAttributes([NSAttributedString.Key.foregroundColor: UIColor.init(hex: 0x4B3800)!], range: NSRange(location: 0, length: nickName.length))
            
            let attrString = NSMutableAttributedString(string: "开通会员获取超多特权")
        
            attrString.addAttributes(content_attr, range: NSRange(location: 0, length: attrString.length))
            
            content_label.attributedText = attrString
            
            let btnAttrString = NSMutableAttributedString(string: "开通会员")
            let attr1: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.medium),.foregroundColor: UIColor.init(hex: 0xFFD654)!]
            btnAttrString.addAttributes(attr1, range: NSRange(location: 0, length: btnAttrString.length))
            click_label.attributedText = btnAttrString
            click_label.backgroundColor = UIColor.init(hex: 0x2C211D)
            
            click_label.isHidden = false
            
            break
        }
        
        
        titleLabel.attributedText = nickName
        
        
    }
    
    
}



/**
 会员状态
 */
enum ST_BTMemberStatus:Int {
    case normal,subscribe,expired
}


class ST_MemberFeatureView: UIView {
    
    static let height = (600+100-80+60).cgFloat
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        

        self.backgroundColor = .white
        
        
    //        功能特权
        self.addSubview(member_feature_label)
        
        member_feature_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(20)
            make.height.equalTo(21)
        }
        
        let array0 = ST_AICenterPrivilegeCellChildBoxModel.setup_feature_data()
        
        
        let featureOffset = TIANCompatibleValue(value: 30)
        
        let meetyouItemView0 = ST_AICenterPrivilegeCellChildBox()
        meetyouItemView0.model = array0[safe:0]
        self.addSubview(meetyouItemView0)
        meetyouItemView0.snp.makeConstraints { (make) in
            make.size.equalTo(ST_AICenterPrivilegeCellChildBox.size)
            make.right.equalTo(self.snp.centerX).offset(-featureOffset)
            make.top.equalTo(member_feature_label.snp.bottom).offset(15)
        }
        
        
        let meetyouItemView1 = ST_AICenterPrivilegeCellChildBox()
        meetyouItemView1.model = array0[safe:1]
        self.addSubview(meetyouItemView1)
        meetyouItemView1.snp.makeConstraints { (make) in
            make.size.equalTo(ST_AICenterPrivilegeCellChildBox.size)
            make.left.equalTo(self.snp.centerX).offset(featureOffset)
            make.top.equalTo(meetyouItemView0)
        }
        
        let meetyouItemView2 = ST_AICenterPrivilegeCellChildBox()
        meetyouItemView2.model = array0[safe:2]
        self.addSubview(meetyouItemView2)
        meetyouItemView2.snp.makeConstraints { (make) in
            make.size.equalTo(ST_AICenterPrivilegeCellChildBox.size)
            make.left.equalTo(meetyouItemView0)
            make.top.equalTo(meetyouItemView0.snp.bottom).offset(15)
        }
        
        let meetyouItemView3 = ST_AICenterPrivilegeCellChildBox()
        meetyouItemView3.model = array0[safe:3]
        self.addSubview(meetyouItemView3)
        meetyouItemView3.snp.makeConstraints { (make) in
            make.size.equalTo(ST_AICenterPrivilegeCellChildBox.size)
            make.right.equalTo(meetyouItemView1)
            make.top.equalTo(meetyouItemView2)
        }
        
        
        let meetyouItemView4 = ST_AICenterPrivilegeCellChildBox()
        meetyouItemView4.model = array0[safe:4]
        self.addSubview(meetyouItemView4)
        meetyouItemView4.snp.makeConstraints { (make) in
            make.size.equalTo(ST_AICenterPrivilegeCellChildBox.size)
            make.left.equalTo(meetyouItemView0)
            make.top.equalTo(meetyouItemView2.snp.bottom).offset(15)
        }
        
        
        let meetyouItemView5 = ST_AICenterPrivilegeCellChildBox()
        meetyouItemView5.model = array0[safe:5]
        self.addSubview(meetyouItemView5)
        meetyouItemView5.snp.makeConstraints { (make) in
            make.size.equalTo(ST_AICenterPrivilegeCellChildBox.size)
            make.right.equalTo(meetyouItemView3)
            make.top.equalTo(meetyouItemView4)
        }
        

    //        身份特权
        self.addSubview(member_label)
        
        member_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
//            make.top.equalToSuperview().offset(20)
            make.top.equalTo(meetyouItemView5.snp.bottom).offset(30)
            make.height.equalTo(21)
        }
        
        
        let array = ST_AICenterPrivilegeCellChildBoxModel.setup_identity_data()
        
        
        let memberItemView1 = ST_AICenterPrivilegeCellChildBox()
        memberItemView1.model = array[safe:1]
        self.addSubview(memberItemView1)
        memberItemView1.snp.makeConstraints { (make) in
            make.size.equalTo(CGSize.init(width: 125, height: 34+101-15))
            make.centerX.equalToSuperview()
            make.top.equalTo(member_label.snp.bottom).offset(15)
        }

        let memberOffset = TIANCompatibleValue(value: 2)
        
        let memberItemView0 = ST_AICenterPrivilegeCellChildBox()
        memberItemView0.model = array[safe:0]
        self.addSubview(memberItemView0)
        memberItemView0.snp.makeConstraints { (make) in
            make.top.size.equalTo(memberItemView1)
            make.right.equalTo(memberItemView1.snp.left).offset(-memberOffset)
        }

        let memberItemView2 = ST_AICenterPrivilegeCellChildBox()
        memberItemView2.model = array[safe:2]
        self.addSubview(memberItemView2)
        memberItemView2.snp.makeConstraints { (make) in
            make.top.size.equalTo(memberItemView1)
            make.left.equalTo(memberItemView1.snp.right).offset(memberOffset)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK:Properties
    
     let member_label_attr: [NSAttributedString.Key : Any] = [.font: UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.semibold),.foregroundColor: UIColor.init(hex: 0x333333)!]
    
    fileprivate lazy var member_label:UILabel = {
        
        let control = UILabel.init()
        
        let attrString = NSMutableAttributedString(string: "身份特权")
        
        attrString.addAttributes(member_label_attr, range: NSRange(location: 0, length: attrString.length))
        
        control.attributedText = attrString
        return control
    }()
    
    
     fileprivate lazy var member_meet_label:UILabel = {
        
        let control = UILabel.init()
        
        let attrString = NSMutableAttributedString(string: "我的遇见")
        
        attrString.addAttributes(member_label_attr, range: NSRange(location: 0, length: attrString.length))
        
        control.attributedText = attrString
        return control
    }()
    
    
    fileprivate lazy var member_feature_label:UILabel = {
        
        let control = UILabel.init()
        
        let attrString = NSMutableAttributedString(string: "功能特权")
        
        attrString.addAttributes(member_label_attr, range: NSRange(location: 0, length: attrString.length))
        
        control.attributedText = attrString
        
        return control
    }()
    
//    fileprivate lazy var rightsDataArray = ST_AICenterMemberCellItemViewModel.itemViewModelList()
    
}



//MAKR: - Cell Item
class ST_AICenterMemberCellItemView: UIView {
    
    
    static let height = 72.cgFloat
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.backgroundColor = .white
        
        self.addSubview(imageView)
        imageView.snp.makeConstraints { (make) in
            make.left.equalToSuperview()
            make.top.equalToSuperview().offset(18)
            make.bottom.equalToSuperview().offset(-18)
            make.width.equalTo(self.imageView.snp.height)
        }
        
        
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(15)
            make.left.equalTo(imageView.snp.right).offset(10)
            make.height.equalTo(21)
        }
        
        self.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(1)
            make.left.equalTo(imageView.snp.right).offset(10)
            make.height.equalTo(20)
        }
        
        self.addSubview(line)
        line.snp.makeConstraints { (make) in
            make.bottom.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(1)
            make.left.equalToSuperview().offset(61)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var line:UIView = {
       let control = UIView()
        control.backgroundColor = UIColor.init(hex: 0xF5F8FA)
        return control
    }()

    fileprivate lazy var imageView:UIImageView = {
        let control = UIImageView()
        control.contentMode = .scaleAspectFill
        return control
    }()

    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.textColor = Color.init(hexString: "#333333")
        control.font = UIFont.systemFont(ofSize: 15, weight: UIFont.Weight.semibold)
        return control
    }()
    
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.textColor = Color.init(hexString: "#61666F")
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.regular)
        return control
    }()
    
    
    func bind(model:ST_AICenterMemberCellItemViewModel){
        
        imageView.image = UIImage.init(named: model.imageName)
        
        titleLabel.text = model.title
        
        content_label.text = model.content
        
    }
    
}


struct ST_AICenterMemberCellItemViewModel {
    var imageName:String
    var title:String
    var content:String
    
    
}






//MARK: - Child
class ST_AICenterPrivilegeCellChildBox: UIView {

    static let size = CGSize.init(width: 100, height: 126)
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(cover_image_view)
        cover_image_view.snp.makeConstraints { (make) in
            make.width.height.equalTo(62)
            make.top.centerX.equalToSuperview()
        }
        
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(5+62)
        }
        
        self.addSubview(content_label)
        content_label.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom).offset(2)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = Color.init(hexString: "#333333")
        control.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.semibold)
        return control
    }()
    
    fileprivate lazy var content_label:UILabel = {
        let control = UILabel.init()
        control.numberOfLines = 0
//        control.backgroundColor = .random
        return control
    }()
    
    fileprivate lazy var content_attribute:[NSAttributedString.Key: Any] = {
        
        let p = NSParagraphStyle.create_line_spacing_paragraph_style(lineSpacing: 2)
        
        p.alignment = .center
        
        return [.foregroundColor:UIColor.init(hex: 0xACB4C0)!,
                .font:UIFont.systemFont(ofSize: 12, weight: UIFont.Weight.regular),.paragraphStyle:p]
        
    }()
    

    fileprivate lazy var cover_image_view:UIImageView = {
         let control = UIImageView()
          control.contentMode = .scaleAspectFill
          return control
      }()
    
    
    var model:ST_AICenterPrivilegeCellChildBoxModel?{
        didSet{
            
            guard let m = model else { return  }
            
            cover_image_view.image = UIImage.init(named: m.imageName)
            
            titleLabel.text = m.title
            
            let text = NSMutableAttributedString.init(string: m.content)
            
            text.addAttributes(self.content_attribute, range: NSRange.init(location: 0, length: text.length))
            
            content_label.attributedText = text
        }
    }
}

struct ST_AICenterPrivilegeCellChildBoxModel {
  
    
    var title:String
    var imageName:String
    var content:String
    
    
    
    
    
    static func setup_status_data()->[ST_AICenterPrivilegeCellChildBoxModel]{
        
        
        let titleArray = ["金色昵称","专属标识","消息直达"]
        
        let contentArray = ["整条gai你最闪亮","会员的牌面","消息直达会话列表"]
        
        var array = [ST_AICenterPrivilegeCellChildBoxModel]()
        
        for (i,title) in titleArray.enumerated(){
            
            let content = contentArray[i]
            
            let m = ST_AICenterPrivilegeCellChildBoxModel.init(title: title, imageName: "特权_身份_icon_\(i)", content: content)
            
            array.append(m)
            
        }
        
        return array
        
    }
    
    
    static func setup_meet_data()->[ST_AICenterPrivilegeCellChildBoxModel]{
        
        
        let titleArray = ["谁喜欢我","谁看过我","过滤最爱","重新接受"]
        
        let contentArray = ["立刻与ta配对","立刻与ta匹配","只看符合我要求","忽略的人重启聊天"]
        
        var array = [ST_AICenterPrivilegeCellChildBoxModel]()
        
        for (i,title) in titleArray.enumerated(){
            
            let content = contentArray[i]
            let m = ST_AICenterPrivilegeCellChildBoxModel.init(title: title, imageName: "特权_遇见_icon_\(i)", content: content)
            array.append(m)
        }
        
        return array
        
    }
    
    /**
     功能特权
     */
    static func setup_feature_data()->[ST_AICenterPrivilegeCellChildBoxModel]{
        
        
        let titleArray = ["每日20次喜欢","查看谁喜欢我","筛选最想看的人","任意修改位置","精准搜索用户"]
        
        let contentArray = ["突破喜欢上限，不错过\n每个机会","立即配对，成为好友","多条件组合筛选，只看\n最满意的人","修改位置漫游全国","输入昵称或特质，精准\n找到你要找的人"]
        
        var array = [ST_AICenterPrivilegeCellChildBoxModel]()
        
        for (i,title) in titleArray.enumerated(){
            
            let content = contentArray[i]
            let m = ST_AICenterPrivilegeCellChildBoxModel.init(title: title, imageName: "新功能特权_遇见_icon_\(i)", content: content)
            array.append(m)
        }
        
        return array
        
    }
    
    /**
     身份特权
     */
    static func setup_identity_data()->[ST_AICenterPrivilegeCellChildBoxModel]{
        
        
        let titleArray = ["金色昵称","开启隐身模式","专属标识"]
        
        let contentArray = ["整条gai你最闪亮","在伴心所有版块隐身，\n不会被所有人看到","会员的牌面"]
        
        var array = [ST_AICenterPrivilegeCellChildBoxModel]()
        
        for (i,title) in titleArray.enumerated(){
            
            let content = contentArray[i]
            let m = ST_AICenterPrivilegeCellChildBoxModel.init(title: title, imageName: "特权_身份_icon_\(i)", content: content)
            array.append(m)
        }
        
        return array
        
    }
}





